<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>评论字数统计</title>
    <style>
        .box {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        h2 {
            color: #333;
            margin-bottom: 20px;
        }

        textarea {
            width: 100%;
            min-height: 100px;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            resize: vertical;
            box-sizing: border-box;
        }

        .ccount {
            margin-top: 10px;
            color: #666;
            font-size: 14px;
        }

        .ccount span {
            color: #1890ff;
            font-weight: bold;
        }

        button {
            margin-top: 15px;
            padding: 10px 30px;
            background-color: #1890ff;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
        }

        button:hover {
            background-color: #40a9ff;
        }

        button:disabled {
            background-color: #ccc;
            cursor: not-allowed;
        }
    </style>
</head>

<body>
    <div class="box">
        <h2>发一条友善的评论</h2>
        <textarea name="字数" class="cInput" placeholder="请输入评论内容！"></textarea>
        <div class="ccount">
            已输入 <span class="sp">0</span> / 200 字
        </div>
        <button class="btu">发布</button>
    </div>

    <script> const commentInput = document.querySelector('cInput');
        const char = document.querySelector('charNum');
        const submit = document.querySelector('submitBtn');

        commentInput.addEventListener('input', function () {

            const length = this.value.length;


            charNum.textContent = length;

            if (length > 200) {
                charNum.style.color = '#ff4d4f';
                submitBtn.disabled = true;
            } else {
                charNum.style.color = '#1890ff';
                submitBtn.disabled = false;
            }
        });
        submitBtn.addEventListener('click', function () {
            const content = commentInput.value.trim();
            if (content === '') {
                alert('请输入评论内容！');
                return;
            }
            if (content.length > 200) {
                alert('评论内容不能超过200字！');
                return;
            }
            alert('评论发布成功！');
            commentInput.value = '';
            charNum.textContent = '0';
        });



    </script>


























    <!-- const commentInput = document.getElementById('cInput');
    const charNum = document.getElementById('charNum');
    const submitBtn = document.getElementById('submitBtn');

    // 监听输入事件
    commentInput.addEventListener('input', function() {
    // 获取当前输入的字数
    const length = this.value.length;

    // 更新字数显示
    charNum.textContent = length;

    // 如果超过200字，禁用发布按钮并改变颜色
    if (length > 200) {
    charNum.style.color = '#ff4d4f';
    submitBtn.disabled = true;
    } else {
    charNum.style.color = '#1890ff';
    submitBtn.disabled = false;
    }
    });

    // 发布按钮点击事件
    submitBtn.addEventListener('click', function() {
    const content = commentInput.value.trim();
    if (content === '') {
    alert('请输入评论内容！');
    return;
    }
    if (content.length > 200) {
    alert('评论内容不能超过200字！');
    return;
    }
    alert('评论发布成功！');
    commentInput.value = '';
    charNum.textContent = '0';
    });
    </script> -->
</body>

</html>